<?php
/**
 * 砍价排行榜
 *
 * Date: 2017/8/2
 * Time: 14:14
 */
defined('InNcPHP') or exit('Access Invalid!');

class bargain_pkControl extends  BaseHomeControl{
    public $redis;

    public $deploy;

    public  function  __construct()
    {   $this->redis = new Redis();
        $this->deploy = C('redis');
        $this->redis->connect($this->deploy['master']['host'],$this->deploy['master']['port']);
        parent::__construct();
        Tpl::setLayout('bargain_layout');
        Tpl::setDir('bargain');
    }

    /**
     * PK榜单列表
     */
    public  function  indexOp(){

        $row = 29;
        $res = $this->mFristAndLastOp(date('Y'),date('m'));
        $condition['add_time'] = array('between',$res['firstday'].','.$res['lastday']);
        $time = date('Ym',time());
        $pxname = $this->deploy['prefix'].'orderpx';
        $res  = $this->redis->zRevRange($pxname,0,$row);
        $pk_list = array();
        foreach($res as $k){
            $order_id = substr($k,9);
            $pk_list[] = $this->redis->hGetAll($this->deploy['prefix'].'month_order'.$order_id);
        }

        if (isset($_GET['isajax']) && $_GET['isajax'] == 1) {
            $page = ($_GET['curpage']-1)*($row+1);

            $res  = $this->redis->zRevRange($pxname,$page,$_GET['curpage']*$row+1);

            $pk_list = array();
            foreach($res as $k){
                $order_id = substr($k,9);
                $pk_list[] = $this->redis->hGetAll($this->deploy['prefix'].'month_order'.$order_id);
            }
            Tpl::output('page',$_GET['curpage']);
            Tpl::output('row',$row);
            Tpl::output('pk_list',$pk_list);
            Tpl::showpage('redis_pk_list_page', 'null_layout');

            exit;
        }


        Tpl::output('pk_list',$pk_list);




        Tpl::output('time',$time);
        Tpl::showpage('pk_index');

    }


    /**
     * PK榜历史记录
     */
    public function history_pklistOp(){
        $order_model = Model('bargain_order');
        $lottery_model = Model('bargain_lottery');
        $condition = array();
        $nian = substr(intval($_GET['time']),0,4);
        $yue = substr(intval($_GET['time']),4);
        $rule_time = $this->mFristAndLastOp($nian,$yue);
        //$condition['add_time'] = array('between',$rule_time['firstday'].','. $rule_time['lastday']);
        $time = intval($_GET['time']);
        $condition['lottery_ym'] = $nian.$yue;
        $pk_list = $lottery_model->getLotteryListPage($condition,'*',20,'in_all desc');
        //$pk_list = $order_model->getBgOrderListPage($condition,'*',10,'in_all desc');
        Tpl::output('pk_list',$pk_list);
        //ajax 加载分页
        if (isset($_GET['isajax']) && $_GET['isajax'] == 1) {

            Tpl::showpage('pk_list_page', 'null_layout');
            exit;
        }

        Tpl::output('TotalNum',pagecmd("gettotalnum"));
        Tpl::output('TotalPage',pagecmd("gettotalpage"));
        Tpl::output('time',$time);
        Tpl::showpage('history_pk_index');



    }


    /**
     * 中奖名单
     */
    public function  lottery_listOp(){

        $lottery_model = Model('bargain_lottery');
        $condition = array();
        $condition['if_lottery'] =1;
        $lottery_list = $lottery_model->getLotteryListPage($condition,'*',10,'lottery_time desc');
        Tpl::output('lottery_list',$lottery_list);
        //ajax 加载分页
        if (isset($_GET['isajax']) && $_GET['isajax'] == 1) {
            Tpl::showpage('lottery_page', 'null_layout');
            exit;
        }
        Tpl::output('TotalNum',pagecmd("gettotalnum"));
        Tpl::output('TotalPage',pagecmd("gettotalpage"));
        Tpl::showpage('lottery_list');

    }






    /**
     * 获取指定月份的第一天开始和最后一天结束的时间戳
     *
     * @param int $y 年份 $m 月份
     * @return array(本月开始时间，本月结束时间)
     */
    Private  function mFristAndLastOp($y = "", $m = ""){
        if ($y == "") $y = date("Y");
        if ($m == "") $m = date("m");
        $m = sprintf("%02d", intval($m));
        $y = str_pad(intval($y), 4, "0", STR_PAD_RIGHT);

        $m>12 || $m<1 ? $m=1 : $m=$m;
        $firstday = strtotime($y . $m . "01000000");
        $firstdaystr = date("Y-m-01", $firstday);
        $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));

        return array(
            "firstday" => $firstday,
            "lastday" => $lastday
        );
    }

}